const fs = require('fs');
const path = require('path');
const SOURCES_PATH = "./creator_sources/platform";  //源目录

let config = fs.readFileSync(path.join(__dirname,'./wdConfig.json'), 'utf8');
config = JSON.parse(config);
let TARGET_PATH=path.join(config.android.platform.projectPath,'/assets/script/platform');
try{
    fs.statSync(path.join(config.android.platform.projectPath,"/assets/script/platform/AndroidPlatform.ts"));
    //如果可以执行到这里那么就表示存在了
    console.log('AndroidPlatform存在了');
}catch(e){
    checkDirectory(SOURCES_PATH,TARGET_PATH,copy)
}

function copy(origin,target) {
    
    fs.readdir(origin, function(err,paths){
        if(err) throw err
        paths.forEach(function (url,index,arr) {
            let _origin=path.join(origin,url);
            let _target=path.join(target,url);
            fs.stat(_origin,function(err,stats){
                if(err) throw err;
                if(stats.isFile()){
                  let readable=fs.createReadStream(_origin);
                  let writable=fs.createWriteStream(_target); 
                  writable.on('close', function () {
                    console.log('复制完成')
                  }); 
                  readable.pipe(writable);
                }else if(stats.isDirectory()){
                    checkDirectory(_origin,_target,copy);
                }
            })
        });

    })
}
function checkDirectory(origin,target,callback) {
    try{
        fs.accessSync(target,fs.constants.F_OK);
        callback(origin,target);
    }catch(err){
        fs.mkdirSync(target);
        callback(origin,target);
    }
    
}
